const Koa = require('koa')
const App = new Koa()
const Logger = require('koa-logger')
const bodyParser = require('koa-bodyparser')
const Mongo = require('./app/lib/mongoose')

// Mongodb 链接全局
try {
	Mongo.connect
	console.log('Mongodb 已经连接')
} catch (error) {
	console.trace(error)
}

// 路由

const Oauth = require('./routes/oauth')
const Rbac = require('./routes/rbac')
// 全局错误处理
App.use(async (ctx, next) => {
	try {
		await next()
	} catch (err) {
		ctx.body = {
            ret: -1,
            msg: err.message
        }
		ctx.status = err.status || 500
	}
})

App.use(Logger());
App.use(bodyParser())

// OAUTH ROUTE
App.use(Oauth.routes());
App.use(Rbac.routes());

App.listen('3000', () => {
    console.log('the application is running at port 3000')
})